// --------------------------------------------------------------------------------------------
//		Project : Debatewise for Google Wave
// --------------------------------------------------------------------------------------------

/*

Javascript classes by Peter Chamberlin

Copyright (c) 2009, debatewise.com
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of debatewise.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

// --------------------------------------------------------------------------------------------

    // Create doc object to replace document - better cross browser compatibility
		if ((document.getElementById)||(document.layers)) {
			var doc = document;
		} else {
			var doc = document.all;
		}

// --------------------------------------------------------------------------------------------
//		Class : DebatewiseDbIO
// --------------------------------------------------------------------------------------------

		function DebatewiseDbIO() {}

// ---Function : adminRequest() ---------------------------------------------------------------

    // Get admin wave content by url call
    DebatewiseDbIO.prototype.adminRequest=function(uid) {    
      var params = {};
      params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
      var url = "http://dan-debatewise.appspot.com/debate/admin?userid="+uid;
			
      // url request to debug div
      if(debug)doc.getElementById("debug-request").innerHTML=url;

      gadgets.io.makeRequest(url, dataIO.adminResponse, params);
    }

// ---Function : adminResponse() --------------------------------------------------------------

    // handle and display admin content in the gadget
    DebatewiseDbIO.prototype.adminResponse=function(get) {

      // DEBUG:
			// if(debug){
	      //check the object is useful, else print error to debug-response
	      // if((!json.title)||((!json.summary)&&(!json.summery))||(!json.startdate)||(!json.creator)||(!json.debates)) { // handle failure to create dom elements
	        // var debugresponse = "<br /><b>Fail: object format incorrect.</b><br />Received: " + get.text + "<br />Expected: json.title, json.summary, json.startdate, json.creator, json.debates[]";
	      // } else {
	        // var debugresponse = get.text;
	      // }
	      // doc.getElementById("debug-response").innerHTML=debugresponse; // print debug-response
			// }
      // END DEBUG

			if(debug) {
				var debugresponse = get.text;
				doc.getElementById("debug-response").innerHTML=debugresponse;
			}
			
      if(!dwHTML.makeAdminHTML(get.text)) { // handle failure to create dom elements
        alert("Fail: could not create admin html");
        return false;
      }

      // fit the gadget to its contents
			gadgets.window.adjustHeight();
    }

// ---Function : indexRequest() ---------------------------------------------------------------

    // Get index wave content by url call
    DebatewiseDbIO.prototype.indexRequest=function(uid) {    
      var params = {};
      params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
      var url = "http://dan-debatewise.appspot.com/debate/index?userid="+uid;

      // url request to debug div
      if(debug)doc.getElementById("debug-request").innerHTML=url;

      gadgets.io.makeRequest(url, dataIO.indexResponse, params);
    }

// ---Function : indexResponse() --------------------------------------------------------------

    // handle and display index content in the gadget
    DebatewiseDbIO.prototype.indexResponse=function(get) {
		
      // handle failure to create dom elements
      if(!dwHTML.makeIndexHTML()){
        alert("Fail: could not create index html");
        return false;
      }

      // Title the gadget
      dwHTML.makeTitles("Debatewise: Index of Debates","This gadget indexes all of the debates in the Copenhagen series. Lorem ipsum summary type content goes here...");

      // Title the tabs
      dwHTML.setTabs("Open debates","Completed debates");

      // parse the response as json
      var json = gadgets.json.parse(get.text);

      // DEBUG:
      // check the object is useful, else return false
      if((!json.title)||((!json.summery)&&(!json.summary))||(!json.startdate)||(!json.creator)||(!json.debates)) { // handle failure to create dom elements
        var debugresponse = "<br /><b>Fail: object format incorrect.</b><br />Received: " + get.text + "<br />Expected: json.title, json.summary, json.startdate, json.creator, json.debates[]";
      } else {
        var debugresponse = get.text;
      }
      doc.getElementById("debug-response").innerHTML=debugresponse; // print debug-response
      // END DEBUG

      doc.getElementById("for-points").innerHTML=json[0].motion;

      // fit the gadget to its contents
			gadgets.window.adjustHeight();
    }

// ---Function : debateRequest() --------------------------------------------------------------

    // Get debate wave content by url call
    DebatewiseDbIO.prototype.debateRequest=function(wid) {    
      var params = {};
      params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
		  var user = wave.getViewer();
		  var uid = user.getId();
      var url = "http://dan-debatewise.appspot.com/debate/meta?userid="+uid+"&debate_key="+key;

      // url request to debug div
      if(debug)doc.getElementById("debug-request").innerHTML=url;

      gadgets.io.makeRequest(url, dataIO.debateResponse, params);
    }

// ---Function : debateResponse() -------------------------------------------------------------
		
    // handle and display debate content in the gadget
    DebatewiseDbIO.prototype.debateResponse=function(get) {

      if(!dwHTML.makeDebateHTML()) { // handle failure to create dom elements
        alert("Fail: could not create index html");
        return false;
      }

      // parse the response as json
      var json = gadgets.json.parse(get.text);
      
      // DEBUG:
      // check the object is useful, else return false
      if(!json.motion) { // handle failure to create dom elements
        var debugresponse ="<br /><b>Fail: object format incorrect.</b><br />Received: " + get.text + "<br />Expected: json.motion, json.summary, json.proposition, json.oppostion, json.points[]";
      } else {
        var debugresponse = get.text;
      }
      doc.getElementById("debug-response").innerHTML=debugresponse; // print debug-response
      // END DEBUG

      doc.getElementById("for-points").innerHTML=json.motion;

      // Title the gadget
      dwHTML.makeTitles("Debate: "+json.motion,"Summary: "+"Proposition: " + json.proposition + " Opposition: " + json.opposition);

      // Title the tabs
      dwHTML.setTabs("Debate points","");

      // fit the gadget to its contents
			gadgets.window.adjustHeight();
    }

// ---Function : pointRequest() ---------------------------------------------------------------

    // Get point wave content by url call
    DebatewiseDbIO.prototype.pointRequest=function(pid) {  
      var params = {};
      params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
      var url = "http://dan-debatewise.appspot.com/debate/point?pointid="+pid;

      // url request to debug div
      if(debug)doc.getElementById("debug-request").innerHTML=url;

      gadgets.io.makeRequest(url, dataIO.pointResponse, params);
    }

// ---Function : pointResponse() --------------------------------------------------------------

    // handle and display point content in the gadget
    DebatewiseDbIO.prototype.pointResponse=function(get) {

      if(!dwHTML.makeAdminHTML()) { // handle failure to create dom elements
        alert("Fail: could not create admin html");
        return false;
      }

      // Title the gadget
      var title = "Debatewise: Admin Console";
      var summary = "This gadget contains the functionality for administration of the debatewise robot.";
      dwHTML.makeTitles(title,summary);

      
      var json = gadgets.json.parse(get.text);

      // DEBUG:
      // check the object is useful, else print error to debug-response
      if((!json.point)||(!json.counterpoint)||(!json.creator)||(!json.debate_key)||(!json.last_editor)) { // handle failure to create dom elements
        var debugresponse ="<br /><b>Fail: object format incorrect.</b><br />Received: " + get.text + "<br />Expected: json.point, json.counterpoint, json.creator, json.debate_key, json.last_editor";
      } else {
        var debugresponse = get.text;
      }
      doc.getElementById("debug-response").innerHTML=debugresponse; // print debug-response
      // END DEBUG

      doc.getElementById("debate-title").innerHTML="Debate Point";
  
      // fit the gadget to its contents
			gadgets.window.adjustHeight();
    }

// --------------------------------------------------------------------------------------------

    var dataIO = new DebatewiseDbIO();

// --------------------------------------------------------------------------------------------